// pages/register/register.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    access_token: '',
    errorUserNoMessage: '',
    errorNameMessage: '',
    errorMobileMessage: '',
    form: {},
    sex_columns: ['女', '男'],
    sex_show: false,
    department_columns: [],
    department_show: false,
    departments:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDepartment()
    let title = '用户注册'
    if(app.globalData.access_token) {
      access_token = app.globalData.access_token
      title = '个人信息'
      let form = app.globalData.userInfo
      form.sex_name = form.sex==0?'女':'男'
      this.setData({
        form, access_token
      })
    }
    wx.setNavigationBarTitle({
      title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onChange(e) {
    if(e.currentTarget.dataset.id == 'user_no') {

    }
    let form = this.data.form
    form[e.currentTarget.dataset.id] = e.detail
    
    this.setData({ form })
  },
  onBindUserNo(e) {
    if(e.detail.cursor != 8) {
      this.setData({
        errorUserNoMessage: '编号必须是8位'
      })
    }else{
      this.setData({
        errorUserNoMessage: ''
      })
    }
  },
  getNumber (res) {
    if(res.detail.errMsg == 'getPhoneNumber:ok') {
      app.requestUrl('/getNumber', {session_key: app.globalData.login.session_key, encryptedData: res.detail.encryptedData, iv: res.detail.iv}, ret=> {
        let form = this.data.form
        form.mobile = ret.phoneNumber
        this.setData({
          form
        })
      }, 'POST')
      
    }else {
      wx.showToast({
        title: '注册必须用手机号！',
        icon: 'none',
        duration: 2000
      }) 
    }
  },
  onShowSex() {
    this.setData({
      sex_show: true
    })
  },
  onShowDepartment() {
    this.setData({
      department_show: true
    })
  },
  onChangeSex(e) {
    let form = this.data.form
    form.sex = e.detail.index
    form.sex_name = e.detail.value
    this.setData({
      form
    })
    this.onClose()
  },
  onChangeDepartment(e) {
    let form = this.data.form
    form.department_name = e.detail.value
    form.department_id = this.data.departments[e.detail.index]['id']
    this.setData({
      form
    })
    this.onClose()
  },
  onClose() {
    this.setData({
      sex_show: false,
      department_show: false
    })
  },
  onConfirm(e) {
    console.log(e)
  },
  submit() {
    let form = this.data.form
    console.log(form)
    if(!form.name || form.name == '') {
      this.setData({
        errorNameMessage: '请填写姓名'
      })
      return 
    }
    if(!form.user_no || form.user_no == '') {
      this.setData({
        errorUserNoMessage: '请填写8位编号'
      })
      return 
    }
    if(!form.mobile || form.mobile == '') {
      this.setData({
        errorMobileMessage: '请点击”获取手机号“'
      })
      return 
    }
    form.openid = app.globalData.login.openid
    let url = '/register'
    if(this.data.access_token) {
      url = '/saveUser'
    }
    app.requestUrl(url, form, ret=> {
      app.globalData.userInfo = ret.user
      let msg = '保存成功！'
      if(!this.data.access_token) {
        msg = '注册成功！'
        app.globalData.access_token = ret.access_token
        wx.setStorage({
          key:"user",
          data:ret.user
        })
                
        wx.setStorage({
          key:"access_token",
          data:ret.access_token
        })

      }
      
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
      })
      setTimeout(function() {
        wx.switchTab({
          url: '/pages/index/index'
        })
      }, 2000)
    }, 'POST')

  },
  getDepartment() {
    app.requestUrl('/getDepartment', {}, ret=> {
      let department_columns = ret.names
      let departments = ret.list
      this.setData({
        department_columns, departments
      })
    }, 'POST')
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})